import unittest
import ddt
import os
from common.base import By
from page.reception.goods.goods_search_page import SearchPage
from common.functions import browser_operation
from common.data_operation import DataOperation

# 准备文件路径
project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
path = os.path.join(project_path, 'data/goods.xlsx')  # join 专门用来路径拼接

# 创建DataOperation对象
goods_list = DataOperation(path).get_data_to_dict()


# 数据驱动
@ddt.ddt
class SearchTestCase(unittest.TestCase):
    # 大夹子
    @classmethod
    def setUpClass(cls) -> None:
        # 打开浏览器
        driver = browser_operation()
        # 创建SearchPage对象
        cls.search = SearchPage(driver)
        # 请求目标网址
        cls.search.get_website(cls.search.index_url)

    # 小夹子
    def tearDown(self) -> None:
        self.search.clear((By.ID, 'keyword'))

    # 数据驱动
    @ddt.data(*goods_list)
    def test_01_case(self, data):
        # 输入商品名
        self.search.goods_input(data['goods_name'])
        # 点击搜索
        self.search.search_btn()

        # 获取标签文本
        text = self.search.get_element_text((By.CLASS_NAME, 'f6'))
        # 断言
        self.assertNotEqual('无法搜索到您要找的商品！', text)

    @classmethod
    def tearDownClass(cls) -> None:
        # 退出
        cls.search.quit()


if __name__ == '__main__':
    unittest.main(verbosity=2)
